/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.accident.service.impl;

import com.koron.accident.domain.Event;
import com.koron.accident.domain.PeccancyRecord;
import com.koron.accident.mapper.EventMapper;
import com.koron.accident.mapper.PeccancyRecordMapper;
import com.koron.accident.service.api.PeccancyRecordService;
import com.koron.accident.util.BusinessCode;
import com.koron.common.domain.DataBean;
import com.koron.common.generator.DefaultIdGenerator;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import java.util.List;

@Service
public class PeccancyRecordServiceImpl implements PeccancyRecordService {
    private final static  Logger logger= LoggerFactory.getLogger(PeccancyRecordServiceImpl.class);
    @Override
    public MessageBean<Integer> save(PeccancyRecord bean) {
        MessageBean<Integer> msg=new MessageBean<>();
        try(SessionFactory factory=new SessionFactory()){
            if (StringUtils.isEmpty(bean.getCorpCode())){
                msg.setCode(BusinessCode.PECCANCYRECORD_EMPTY_CORPCODE.getCode());
                msg.setDescription(BusinessCode.PECCANCYRECORD_EMPTY_CORPCODE.getDescription());
                return msg;
            }
            if (StringUtils.isEmpty(bean.getPeccancyStaff())||StringUtils.isEmpty(bean.getPeccancyStaffName())){
                msg.setCode(BusinessCode.PECCANCYRECORD_EMPTY_PECCANCYSTAFF.getCode());
                msg.setDescription(BusinessCode.PECCANCYRECORD_EMPTY_PECCANCYSTAFF.getDescription());
                return msg;
            }
            if (StringUtils.isEmpty(bean.getPeccancyStaffDept())||StringUtils.isEmpty(bean.getPeccancyStaffDeptName())){
                msg.setCode(BusinessCode.PECCANCYRECORD_EMPTY_PECCANCYSTAFFDEPT.getCode());
                msg.setDescription(BusinessCode.PECCANCYRECORD_EMPTY_PECCANCYSTAFFDEPT.getDescription());
                return msg;
            }
            if (StringUtils.isEmpty(bean.getPeccancyBehaviour())){
                msg.setCode(BusinessCode.PECCANCYRECORD_EMPTY_PECCANCYBEHAVIOUR.getCode());
                msg.setDescription(BusinessCode.PECCANCYRECORD_EMPTY_PECCANCYBEHAVIOUR.getDescription());
                return msg;
            }
            if (StringUtils.isEmpty(bean.getBusinessId())){
                msg.setCode(BusinessCode.PECCANCYRECORD_EMPTY_BUSINESSID.getCode());
                msg.setDescription(BusinessCode.PECCANCYRECORD_EMPTY_BUSINESSID.getDescription());
                return msg;
            }
            if (null==bean.getPeccancyDegree()){
                msg.setCode(BusinessCode.PECCANCYRECORD_EMPTY_PECCANCYDEGREE.getCode());
                msg.setDescription(BusinessCode.PECCANCYRECORD_EMPTY_PECCANCYDEGREE.getDescription());
                return msg;
            }
            if (null==bean.getPeccancyType()){
                msg.setCode(BusinessCode.PECCANCYRECORD_EMPTY_PECCANCYTYPE.getCode());
                msg.setDescription(BusinessCode.PECCANCYRECORD_EMPTY_PECCANCYTYPE.getDescription());
                return msg;
            }
            Event event=factory.getMapper(EventMapper.class).queryOne(bean.getBusinessId());
            //判断关联的事故报告是否存在
            if (null==event){
                msg.setCode(BusinessCode.PECCANCYRECORD_NOT_EXIST_BUSINESSID.getCode());
                msg.setDescription(BusinessCode.PECCANCYRECORD_NOT_EXIST_BUSINESSID.getDescription());
                return msg;
            }
            Integer value=0;
            if(StringUtils.isEmpty(bean.getId())){
                //新增
                bean.setId(DefaultIdGenerator.getInstance().generateLongId());
                bean.setAddTime(bean.getUpdateTime());
                bean.setAddUser(bean.getAddUser());
                bean.setAddUserName(bean.getUpdateUserName());
                value=factory.getMapper(PeccancyRecordMapper.class).insert(bean);
            }else{
                //修改
                PeccancyRecord entity=factory.getMapper(PeccancyRecordMapper.class).queryOne(bean.getId());
                if (null==entity){
                    msg.setCode(BusinessCode.PECCANCYRECORD_NOT_EXIST.getCode());
                    msg.setDescription(BusinessCode.PECCANCYRECORD_NOT_EXIST.getDescription());
                    return msg;
                }
                value=factory.getMapper(PeccancyRecordMapper.class).update(bean);
            }
            if (value>0){
                msg.setCode(BusinessCode.SUCCESS.getCode());
                msg.setDescription(BusinessCode.SUCCESS.getDescription());
                msg.setData(value);
                return msg;
            }else {
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription(BusinessCode.FAILURE.getDescription());
                msg.setData(value);
                return msg;
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

    @Override
    public MessageBean<Integer> delete(String id) {
        MessageBean<Integer> msg=new MessageBean<>();
        try(SessionFactory factory=new SessionFactory()){
            if (StringUtils.isEmpty(id)){
                msg.setCode(BusinessCode.PECCANCYRECORD_NOT_CHANGE_DATA.getCode());
                msg.setDescription(BusinessCode.PECCANCYRECORD_NOT_CHANGE_DATA.getDescription());
                return msg;
            }
            PeccancyRecord entity=factory.getMapper(PeccancyRecordMapper.class).queryOne(id);
            if (null==entity){
                msg.setCode(BusinessCode.PECCANCYRECORD_NOT_EXIST.getCode());
                msg.setDescription(BusinessCode.PECCANCYRECORD_NOT_EXIST.getDescription());
                return msg;
            }
            Integer value=factory.getMapper(PeccancyRecordMapper.class).delete(id);
            if (value>0){
                msg.setCode(BusinessCode.SUCCESS.getCode());
                msg.setDescription(BusinessCode.SUCCESS.getDescription());
                msg.setData(value);
                return msg;
            }else {
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription(BusinessCode.FAILURE.getDescription());
                msg.setData(value);
                return msg;
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

    @Override
    public MessageBean<PeccancyRecord> queryOne(String id) {
        MessageBean<PeccancyRecord> msg=new MessageBean<>();
        try(SessionFactory factory=new SessionFactory()){
            if (StringUtils.isEmpty(id)){
                msg.setCode(BusinessCode.PECCANCYRECORD_NOT_CHANGE_DATA.getCode());
                msg.setDescription(BusinessCode.PECCANCYRECORD_NOT_CHANGE_DATA.getDescription());
                return msg;
            }
            PeccancyRecord record=factory.getMapper(PeccancyRecordMapper.class).queryOne(id);
            msg.setCode(BusinessCode.SUCCESS.getCode());
            msg.setDescription(BusinessCode.SUCCESS.getDescription());
            msg.setData(record);
            return msg;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

    @Override
    public MessageBean<DataBean<PeccancyRecord>> list(String businessId, Integer start, Integer rows) {
        MessageBean<DataBean<PeccancyRecord>> msg=new MessageBean<>();
        try(SessionFactory factory=new SessionFactory()){
            List<PeccancyRecord> list=factory.getMapper(PeccancyRecordMapper.class).queryByBusinessId(businessId,start,rows);
            Integer totalNumber=factory.getMapper(PeccancyRecordMapper.class).countByBusinessId(businessId);
            rows = rows == null ?totalNumber:( rows <= 0 ? 1 : rows);
            Integer totalPage=(totalNumber>rows)?(totalNumber%rows==0?totalNumber/rows:totalNumber/rows+1):1;
            DataBean<PeccancyRecord> dataBean=new DataBean<>();
            dataBean.setPageSize(rows);
            dataBean.setTotalPage(totalPage);
            dataBean.setTotalNumber(totalNumber);
            dataBean.setList(list);
            msg.setData(dataBean);
            msg.setCode(BusinessCode.SUCCESS.getCode());
            msg.setDescription(BusinessCode.SUCCESS.getDescription());
            return msg;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }
}
